<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>空间分析服务-栅格代数运算</title>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" src="../../dist/include-leaflet.js"></script>
<script type="text/javascript">
    var host = window.isLocal ? document.location.protocol + "//" + document.location.host : "http://support.supermap.com.cn:8090";
    var map, mathExpressionAnalysisService, mathExpressionAnalysisParameters, mathExpressionAnalysisResult,
        themeService, themeGridRange, themeParameters, themeLayer,
        baseUrl = host + "/iserver/services/map-jingjin/rest/maps/京津地区地图",
        serviceUrl = host + "/iserver/services/spatialanalyst-sample/restjsr/spatialanalyst";
    map = L.map('map', {
        crs: L.CRS.EPSG4326,
        center: [40.05, 116.85],
        maxZoom: 18,
        zoom: 6
    });
    L.supermap.tiledMapLayer(baseUrl, {noWrap: true}).addTo(map).on("load", function () {
        mathExpressionAnalysisProcess();
    });

    function mathExpressionAnalysisProcess() {
        //创建栅格代数运算服务实例
        mathExpressionAnalysisService = L.supermap.spatialAnalystService(serviceUrl);
        //创建栅格代数运算参数实例
        mathExpressionAnalysisParameters = new SuperMap.MathExpressionAnalysisParameters({
            //指定数据集,必设
            dataset: "JingjinTerrain@Jingjin",
            //要执行的栅格运算代数表达式,必设
            expression: "[Jingjin.JingjinTerrain] + 600",
            //存储结果数据集的数据源,必设
            targetDatasource: "Jingjin",
            //结果数据集名称,必设
            resultGridName: "MathExpressionAnalysis_Result",
            deleteExistResultDataset: true
            /*//指定数据集中参与栅格代数运算的区域
             extractRegion: L.polygon([
             [38, 116],
             [38, 117],
             [42, 117],
             [42, 116]
             ])*/
        });
        //向iServer发起栅格代数运算请求
        mathExpressionAnalysisService.mathExpressionAnalysis(mathExpressionAnalysisParameters, function (serviceResult) {
            mathExpressionAnalysisResult = serviceResult.result;
            //用栅格专题图展示分析结果
            showAnalysisResult_ThemeGridRange();
        });
    }

    function showAnalysisResult_ThemeGridRange() {
        themeService = L.supermap.themeService(baseUrl);
        var themeGridRangeIteme1 = new SuperMap.ThemeGridRangeItem({
                start: 0,
                end: 650,
                color: new SuperMap.ServerColor(40, 140, 40)
            }),
            themeGridRangeIteme2 = new SuperMap.ThemeGridRangeItem({
                start: 650,
                end: 750,
                color: new SuperMap.ServerColor(170, 198, 40)
            }),
            themeGridRangeIteme3 = new SuperMap.ThemeGridRangeItem({
                start: 750,
                end: 900,
                color: new SuperMap.ServerColor(191, 191, 0)
            }),
            themeGridRangeIteme4 = new SuperMap.ThemeGridRangeItem({
                start: 900,
                end: 1350,
                color: new SuperMap.ServerColor(191, 95, 0)
            }),
            themeGridRangeIteme5 = new SuperMap.ThemeGridRangeItem({
                start: 1350,
                end: 3600,
                color: new SuperMap.ServerColor(127, 0, 0)
            });
        themeGridRange = new SuperMap.ThemeGridRange({
            reverseColor: false,
            rangeMode: SuperMap.RangeMode.EQUALINTERVAL,
            //栅格分段专题图子项数组
            items: [themeGridRangeIteme1,
                themeGridRangeIteme2,
                themeGridRangeIteme3,
                themeGridRangeIteme4,
                themeGridRangeIteme5
            ]
        });
        themeParameters = new SuperMap.ThemeParameters({
            //制作专题图的数据集
            datasetNames: [mathExpressionAnalysisResult.dataset.split('@')[0]],
            dataSourceNames: ["Jingjin"],
            joinItems: null,
            themes: [themeGridRange]
        });
        themeService.getThemeInfo(themeParameters, function (serviceResult1) {
            var result = serviceResult1.result;
            if (result && result.newResourceID) {
                themeLayer = L.supermap.tiledMapLayer(baseUrl, {
                    noWrap: true,
                    cacheEnabled: false,
                    transparent: true,
                    layersID: result.newResourceID,
                    opacity: 0.8
                }).addTo(map);
            }
        }).on('failed', function (error) {
            alert(error.errorMsg);
        });
    }
</script>
</body>
</html>